package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @PostMapping
    public Result add(@RequestBody Dept dept){
        System.out.println("新增部门："+dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping
    public Result list(String name,Integer status ,
                    @RequestParam (defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println("查询部门："+name+"-"+status+"-"+page+"-"+pageSize);

        PageResult<Dept> pageResult = deptService.list(name,status,page,pageSize);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
            Dept dept = deptService.getById(id);
            return Result.success(dept);
    }
    @PutMapping
    public Result putById(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        deptService.deleteById(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result getAll(){
        List<Dept> deptList = deptService.getAll();
        return Result.success(deptList);
    }

}
